﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Museum.DBAccess;
using System.Data.SqlClient;

public partial class ManageGame : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            BindGridData();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    /// <summary>
    /// Delete records from database
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void DeleteRecord(object sender, GridViewDeleteEventArgs e)
    {
        int gameId = Convert.ToInt32(GridViewGames.DataKeys[e.RowIndex].Value);
        int result = 0;
        try
        {
            result = DBAccess.DeleteGameData(gameId);
            Alert.ShowMessage("Record Deleted successfully.");
            // Refresh the data
            BindGridData();
        }
        catch (SqlException ex)
        {
            lblMessage.Text = ex.Message;
        }
        finally
        {
        }
    }

    /// <summary>
    /// Binds my grid data.
    /// </summary>
    private void BindGridData()
    {
        DataTable dtGames = new DataTable();
        try
        {
            if (Session["SchoolId"] != null && Session["IsSchool"] != null) 
            {
                if (Convert.ToInt32(Session["SchoolId"]) > 0 && Session["IsSchool"].ToString().Equals("1"))
                {
                    dtGames = DBAccess.GetGamesBySchool(Convert.ToInt32(Session["SchoolId"]));
                }
            }
            else if (Session["SchoolId"] != null && Session["Role"] != null)
            {
                if (Convert.ToInt32(Session["SchoolId"]) > 0 && Session["Role"].ToString().Equals("2"))
                {
                    dtGames = DBAccess.GetGamesBySchool(Convert.ToInt32(Session["SchoolId"]));
                }
                else if (Session["MuseumId"] != null)
                {
                    if (Convert.ToInt32(Session["MuseumId"]) > 0)
                    {
                        dtGames = DBAccess.GetAllGamesByMuseum(Convert.ToInt32(Session["MuseumId"]));
                    }
                }
            }
            else if (Session["MuseumId"] != null)
            {
                if (Convert.ToInt32(Session["MuseumId"]) > 0)
                {
                    dtGames = DBAccess.GetAllGamesByMuseum(Convert.ToInt32(Session["MuseumId"]));
                }
            }
            else
                dtGames = DBAccess.GetAllGames();

            //assign DataTable to GridView
            GridViewGames.DataSource = dtGames;
            ViewState["gvData"] = dtGames;
            GridViewGames.DataBind();
        }
        catch
        {
        }
        finally
        {
        }
    }

    private string ConvertSortDirectionToSql(SortDirection sortDirection)
    {
        string newSortDirection = String.Empty;

        switch (sortDirection)
        {
            case SortDirection.Ascending:
                newSortDirection = "ASC";
                break;

            case SortDirection.Descending:
                newSortDirection = "DESC";
                break;
        }

        return newSortDirection;
    }

    protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridViewGames.PageIndex = e.NewPageIndex;
        if (ViewState["gvData"] != null)
            GridViewGames.DataSource = (DataTable)ViewState["gvData"];
        GridViewGames.DataBind();
    }

    protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
    {
        DataTable dataTable = GridViewGames.DataSource as DataTable;

        if (dataTable != null)
        {
            DataView dataView = new DataView(dataTable);
            dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);

            GridViewGames.DataSource = dataView;
            GridViewGames.DataBind();
        }
    }
}
